﻿using PrintPeceipt.Utility;
using Spire.Pdf.Exporting.XPS.Schema;
using Spire.Xls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrintPeceipt
{
    public partial class MainForm : Form
    {
        private string excelPath = "";
        private string bmpPath = "";

        private int width_p = 214;//单位是mm
        private int height_p = 138;//单位是mm
        //private int margin_lr = 10;//左右边距
        //private int margin_tb = 0;//上下边距

        Font mainFont = new Font("宋体", 12);//打印的字体 

        public Peceipts peceipts;


        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 转换毫米到百分之一英寸
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        private int MM2Inch(double mm)
        {
            return (int)(mm * 100.0f / 25.4f);
        }

        ///// <summary>
        ///// 打印事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void pdControl_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //{

        //}

        //private void pdControl_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
        //{

        //}

        ///// <summary>
        ///// 打印开始事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void pdControl_BeginPrint(object sender, PrintEventArgs e)
        //{
        //}

        ///// <summary>
        ///// 打印结束事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void pdControl_EndPrint(object sender, PrintEventArgs e)
        //{

        //}

        private void btnImport_Click(object sender, EventArgs e)
        {
            ImportForm importForm = new ImportForm(this);
            importForm.ShowDialog();
            DataToUI();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            peceipts = new Peceipts();
            DataToUI();
        }

        private void DataToUI()
        {
            this.ReceiptNum.Text = peceipts.ReceiptNum;
            this.Title.Text = peceipts.Title;
            this.ReceiptDate.Text = peceipts.ReceiptDate;
            this.Customer.Text = peceipts.Customer;
            this.Number.Text = peceipts.Number;
            this.Remarks.Text = peceipts.Remarks;
            this.Address.Text = peceipts.Address;
            this.Delivery.Text = peceipts.Delivery;
            this.Consignee.Text = peceipts.Consignee;
            this.Contact.Text = peceipts.Contact;
            this.Wechat.Text = peceipts.Wechat;
            this.dgPeceiptList.DataSource = peceipts.PeceiptList;
        }

        private void UIToData()
        {
            peceipts = new Peceipts();
            peceipts.ReceiptNum = this.ReceiptNum.Text;
            peceipts.Title = this.Title.Text;
            peceipts.ReceiptDate = this.ReceiptDate.Text;
            peceipts.Customer = this.Customer.Text;
            peceipts.Number = this.Number.Text;
            peceipts.Remarks = this.Remarks.Text;
            peceipts.Address = this.Address.Text;
            peceipts.Delivery = this.Delivery.Text;
            peceipts.Consignee = this.Consignee.Text;
            peceipts.Contact = this.Contact.Text;
            peceipts.Wechat = this.Wechat.Text;
            peceipts.PeceiptList = (Peceipt[])this.dgPeceiptList.DataSource;
        }

        private bool DataToFile()
        {
            UIToData();
            if (peceipts.PeceiptList == null)
            {
                MessageBox.Show("请导入数据");
                return false;
            }
            if (peceipts.PeceiptList.Count() == 0)
            {
                MessageBox.Show("请导入数据");
                return false;
            }

            try
            {
                string folder = System.Environment.CurrentDirectory;
                folder += "\\Temp";
                if (!Directory.Exists(folder))
                {
                    DirectoryInfo DirInfo = Directory.CreateDirectory(folder);
                }
                string name = Guid.NewGuid().ToString();

                excelPath = System.IO.Path.Combine(folder, name + ".xlsx");
                bmpPath = System.IO.Path.Combine(folder, name + ".bmp");
                ExcelHelper.CreateExcel(peceipts, excelPath);
                //ExcelHelper.ChangeExcelToImage(excelPath, bmpPath);

            }
            catch (Exception)
            {
                MessageBox.Show("数据异常");
                return false;
            }
            return true;
        }

        private void btnGeneral_Click(object sender, EventArgs e)
        {
            if (DataToFile())
            {

                Workbook workbook = new Workbook();
                workbook.LoadFromFile(excelPath);

                //获取第一个Excel工作表
                Worksheet sheet = workbook.Worksheets[0];
                //设置打印方向
                sheet.PageSetup.PrintTitleRows = "$1:$4"; // 显示第1至9列。
                sheet.PageSetup.PrintTitleColumns = "$A:$I";
                //sheet.PageSetup.Orientation = PageOrientationType.Portrait;
                sheet.PageSetup.Orientation = PageOrientationType.Landscape; //横向适配excel

                sheet.PageSetup.CenterHorizontally = true;//水平居中
                //sheet.PageSetup.Zoom = 200;//打印时页面设置,缩放比例
                sheet.PageSetup.TopMargin = 0; //上边距为0
                sheet.PageSetup.BottomMargin = 0; //下边距为0
                sheet.PageSetup.LeftMargin = 0; //左边距为0
                sheet.PageSetup.RightMargin = 0; //右边距为0

                //sheet.PageSetup.FitToPagesTall = 0;
                sheet.PageSetup.FitToPagesWide = 1; //页宽
                //sheet.PageSetup.Zoom = 10;
                sheet.PageSetup.CenterFooter = "第 &P 页 / 共 &N 页";
                //设置打印区域
                //sheet.PageSetup.PrintArea = string.Format("A1:I{0}", peceipts.PeceiptList.Length + 8);
                //打印顺序
                //sheet.PageSetup.Order = OrderType.OverThenDown;
                //sheet.PageSetup.Order = OrderType.DownThenOver;

                //sheet.ViewMode = ViewMode.Normal;
                int width_in = MM2Inch(width_p);
                int height_in = MM2Inch(height_p);
                //workbook.PrintDocument

                //workbook.PrintDocument.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.pdControl_BeginPrint);
                //workbook.PrintDocument.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.pdControl_EndPrint);
                //workbook.PrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pdControl_PrintPage);
                //workbook.PrintDocument.QueryPageSettings += new System.Drawing.Printing.QueryPageSettingsEventHandler(this.pdControl_QueryPageSettings);

                //workbook.Save();
                //this.pdControl = workbook.PrintDocument;
                //this.pdControl.DefaultPageSettings.PaperSize = new PaperSize("customer", width_in, height_in);
                //int margin_lr_in = MM2Inch(margin_lr);
                //int margin_tb_in = MM2Inch(margin_tb);
                //workbook.PrintDocument.DefaultPageSettings.Margins = new Margins(margin_lr_in, margin_lr_in, margin_tb_in, margin_tb_in);

                ////设置打印对话框属性
                //PrintDialog dialog = new PrintDialog();
                //dialog.Document = this.pdControl;
                //workbook.PrintDialog = dialog;
                //if (DialogResult.OK == dialog.ShowDialog()) //如果确认，将会覆盖所有的打印参数设置
                //{
                //打印预览
                var ppd = new PrintPreviewDialog();
                ppd.Document = workbook.PrintDocument;
                ppd.PrintPreviewControl.Zoom = 0.25;
                if (DialogResult.OK == ppd.ShowDialog())
                {
                    workbook.PrintDocument.Print(); //打印
                }
                //}
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (DataToFile())
            {

                Workbook workbook = new Workbook();
                workbook.LoadFromFile(excelPath);

                //获取第一个Excel工作表
                Worksheet sheet = workbook.Worksheets[0];
                //设置打印方向
                sheet.PageSetup.PrintTitleRows = "$1:$4"; // 显示第1至9列。
                sheet.PageSetup.PrintTitleColumns = "$A:$I";
                sheet.PageSetup.Orientation = PageOrientationType.Landscape; //横向适配excel
                sheet.PageSetup.FitToPagesTall = 0;
                sheet.PageSetup.FitToPagesWide = 1;
                sheet.PageSetup.CenterFooter = "第 &P 页 / 共 &N 页";
                //设置打印区域
                sheet.PageSetup.PrintArea = string.Format("A1:I{0}", peceipts.PeceiptList.Length + 8);
                int width_in = MM2Inch(width_p);
                int height_in = MM2Inch(height_p);
                workbook.PrintDocument.DefaultPageSettings.PaperSize = new PaperSize("customer", width_in, height_in);

                //int margin_lr_in = MM2Inch(margin_lr);
                //int margin_tb_in = MM2Inch(margin_tb);
                //workbook.PrintDocument.DefaultPageSettings.Margins = new Margins(margin_lr_in, 0, margin_tb_in, margin_tb_in);
                workbook.Save();
                workbook.PrintDocument.Print(); //打印
            }
        }
    }
}
